<template>
  <!-- 模板必须有一个唯一的根标签 -->
  <div class="login-container">
    <!-- 水平垂直中居中:1 定位;flex弹性布局 -->
    <div class="login-box">
      <h3 class="title">小U商城后台管理系统</h3>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
      <!-- prop属性是用来和表单校验规则关联的属性 -->
        <el-form-item prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off" placeholder="请输入用户名">
              <template slot="prepend">
                  <i class="el-icon-user"></i>
              </template>
          </el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input type="password" v-model="ruleForm.password" show-password autocomplete="off" placeholder="请输入密码">
               <template slot="prepend">
                    <i class="el-icon-lock"></i>
               </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn-login" type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    
    return {
        // 和表单进行数据绑定的变量
      ruleForm: {
        username: "",
        password: ""
      },
    //   表单元素校验规则
      rules: {
        username: [{ required:true, message:'请输入用户名',trigger: "blur" }],
        password: [{ required:true, message:'请输入密码',trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      // 表单校验的过程
      this.$refs[formName].validate(valid => {
        // 说明表单校验是OK的
        if (valid) {
          this.$http.post('/api/userlogin',this.ruleForm).then(res=>{
            console.log(res);
            if(res.code==200){
              this.$success('登录成功');
              // 将数据接口返回的登录信息保存起来
              sessionStorage.setItem('userinfo',JSON.stringify(res.list));
               // 成功
              this.$router.push('/');
            }else{
              this.$error('登录失败');
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.login-container {
  /* vh:css3中的一个新单位, 屏幕高度==100vh */
  height: 100vh;
  min-width: 970px;
  background: url("../assets/img/loginbg.jpeg");
  /* 覆盖 */
  background-size: cover;
  /* 开启flex布局 */
  display: flex;
  justify-content: center;
  align-items: center;
  
}
.login-box {
  background: #fff;
  width: 400px;
  height: 300px;
  border-radius: 5px;
  padding:30px;
}
.title{
    text-align: center;
    margin-bottom: 30px;
    color:#409EFF;
}
.btn-login{
    width:100%;
}
</style>